package com.bwie.parkdemo.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.bwie.lib_core.base.BaseEntity;
import com.bwie.parkdemo.mvvm.model.entity.LoginEntity;
import com.bwie.parkdemo.mvvm.model.request_entity.UserEntity;

import java.util.List;

/**
 * 全部数据库操作增删改查工具类
 * */
public class DaoHelperUtils extends SQLiteOpenHelper {

    public DaoHelperUtils( Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, "park.db", factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //用户持久化存储表
        db.execSQL("create table sys_user (uId integer not null primary key," +
                "pId integer not null," +
                "uName text not null," +
                "uPwd text not null," +
                "time text not null," +
                "token text not null );");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    //添加数据
    protected int insert(String table, ContentValues values){
        SQLiteDatabase db = getWritableDatabase();
        if (db == null)
            return -1;
        long index = db.insert(table,null,values);
        db.close();
        return (int) index;
    }
    //删除数据
    protected int del(String table, String whereClause, String[] whereArgs){
        SQLiteDatabase db = getWritableDatabase();
        if (db == null)
            return -1;
        int index = db.delete(table,whereClause,whereArgs);
        db.close();
        return index;
    }
    //修改数据
    protected int update(String table, ContentValues values, String whereClause, String[] whereArgs){
        SQLiteDatabase db = getWritableDatabase();
        if (db == null)
            return  -1;
        db.update(table, values, whereClause, whereArgs);
        return 0;
    }
    //查询多条数据->列表数据缓存
    protected List<BaseEntity> selAll(String sqlStr){
        SQLiteDatabase db = getReadableDatabase();
        db.close();
        return null;
    }
    //查询1条数据->单条数据缓存->用户信息
    protected LoginEntity.Values selUser(String sqlStr){
        SQLiteDatabase db = getReadableDatabase();
        LoginEntity.Values entity = new LoginEntity.Values();
        Cursor cursor = db.rawQuery(sqlStr,null);
        while (cursor.moveToNext()){
            entity.uId = cursor.getInt(cursor.getColumnIndex("uId"));
            entity.pId = cursor.getInt(cursor.getColumnIndex("pId"));
            entity.uName = cursor.getString(cursor.getColumnIndex("uName"));
            entity.uPwd = cursor.getString(cursor.getColumnIndex("uPwd"));
            entity.time = cursor.getString(cursor.getColumnIndex("time"));
            entity.token = cursor.getString(cursor.getColumnIndex("token"));
        }
        //关闭cursor游标
        cursor.close();
        db.close();
        return entity;
    }
}
